package app.cn.com.school.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import app.cn.com.school.R
import app.cn.com.school.databinding.ActivityPublishBinding
import app.cn.com.school.tools.Constants
import app.cn.com.school.tools.GetPathVideo
import app.cn.com.school.tools.MyBottomSheetDialog
import app.cn.com.school.tools.PhotoTools

class PublishActivity : BaseActivity<ActivityPublishBinding>() {
    override fun initBindingVar() {
    }

    override fun initTitle() {
        binding.titlebar.title.text = "发布信息"
        binding.titlebar.back.setOnClickListener { finish() }
    }

    var uriPath: Uri? = null
    lateinit var context: Context
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initBinding(R.layout.activity_publish)
        Constants.verifyStoragePermissions(this@PublishActivity)
        context = this
        initView()
    }

    private fun initView() {
        binding.pubGo.setOnClickListener { startActivity(Intent(context, SortActivity::class.java).putExtra("type", "2")) }

        binding.pubSim.setOnClickListener {
            uriPath = PhotoTools.takePhoto(this@PublishActivity)
        }

        binding.pubSim1.setOnClickListener {
            PhotoTools.getImageFromAlbum(this@PublishActivity)
        }
        binding.pubSim2.setOnClickListener {
            ImgDialog()
        }
    }

    private fun ImgDialog() {
        MyBottomSheetDialog(this).build().setFirstItem("拍照", View.OnClickListener { uriPath = PhotoTools.takePhoto(this@PublishActivity) })
                .setSecondItem("从手机相册选择", View.OnClickListener { PhotoTools.getImageFromAlbum(this@PublishActivity) }).show()
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == PhotoTools.ALBUM) {
            if (resultCode == Activity.RESULT_OK) {
                val uri = data!!.data
                binding.pubSim1.setImageURI("file://" + GetPathVideo.getPath(context, uri))
//                binding.storeShopLogo.setImageURI("file://" + GetPathVideo.getPath(context, uri))
//                var bm = ImageFactory.getSmallBitmap(GetPathVideo.getPath(context, uri))
//                shopIcon = ImageFactory.saveFile(bm, "shape.jpg")
//                var shopIcon: File? = null
            }
        }


        if (requestCode == PhotoTools.PHOTO) {
            if (resultCode == Activity.RESULT_OK) {
                binding.pubSim2.setImageURI("file://" + GetPathVideo.getPath(context, uriPath))
            }
        }


    }


}
